/**
 * 
 */
package com.android.dwt.fragment;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.android.dwt.R;
import com.android.dwt.data.GTDApp;
import com.android.dwt.ui.MainAct;
import com.android.dwt.ui.MgrPositionAct;
import com.android.dwt.ui.SettingAct;
import com.android.dwt.ui.ViewTaskAct;
import com.android.dwt.util.C;

/**
 * @author xiang
 *
 */
public class MoreFrag extends Fragment {
	
	private final String TAG = "MoreFrag";
	/*private final int BACKUP = 0;
	private final int RESTORE = 1;
	
	private GTDApp mGa;
	private String mStrRingtone;
	private String mStrVibration;
	private String mStrClearHistory;
	
	private Uri mRingtoneUri = null;		// default silent
	private Boolean mVibration = true;
	private int mGpsRemind;
	
	private RingtonePreference mRingtone;
	private Preference mClearHistory;
	private Preference mBackup;
	private Preference mRestore;
	private CheckBoxPreference mPosRemind;*/
	
	private GTDApp mGa;
	private MainAct mMain;
	private GridView mGridView;
	private ArrayList<HashMap<String, Object>> mData = new ArrayList<HashMap<String,Object>>();
	private SimpleAdapter mAdapter;
	

	/*public Boolean isVibration() {
		if (mVibration)
			return true;
		else
			return false;
	}
	
	public Uri getRingtone() {
		return mRingtoneUri;
	}*/
	
	public static MoreFrag newInstance() {
		
		MoreFrag frag = new MoreFrag();
		
		return frag;
	}
	
	@Override
	public void onAttach(Activity activity) {
		
		super.onAttach(activity);
		
		mMain = (MainAct) activity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
//		setContentView(R.layout.panel_more);
		
		mGa = (GTDApp) mMain.getApplication();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
//		return super.onCreateView(inflater, container, savedInstanceState);
		
		View view = inflater.inflate(R.layout.panel_more, container, false);
		
		return view;
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		
		super.onViewCreated(view, savedInstanceState);
		
		mGridView = (GridView) view.findViewById(R.id.gd_more);
		
		fillData();
		
		mAdapter = new SimpleAdapter(
				mMain,
				mData,
                R.layout.grid_item,
                new String[] { "item_image", "item_text" },   
                new int[] { R.id.item_image, R.id.item_text }
		);
		mGridView.setAdapter(mAdapter);
		
		updateTrash();
		
//		addPreferencesFromResource(R.xml.preferences);
		
//		getKeyString();
//		initPreference();
		
//		registerListener();
		
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@SuppressWarnings("unchecked")
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				HashMap<String, Object> item = (HashMap<String, Object>) parent.getItemAtPosition(position);
				
				String text = (String) item.get("item_text");
				Log.e(TAG, "click " + text);
				
				launchActivity(position);
			}
			
		});
		
		return;
	}
	
	public void updateTrash() {
		
		HashMap<String, Object> hm = (HashMap<String, Object>) mData.get(1);
		
		Cursor csr = mGa.getDbta().getTrash();
		if (csr.getCount() > 0) {
			
			hm.put("item_image", R.drawable.trash_full);
			
		} else {
			
			hm.put("item_image", R.drawable.trash_empty);
		}
		
		mAdapter.notifyDataSetChanged();
		
		csr.close();
		
		return;
	}
	
	private void fillData() {
		
		HashMap<String, Object> hm;
		
		mData.clear();
		
		hm = new HashMap<String, Object>();
		hm.put("item_image", R.drawable.completed);
		hm.put("item_text", "已完成");
		mData.add(hm);
		
		hm = new HashMap<String, Object>();
		hm.put("item_image", R.drawable.trash_empty);
		hm.put("item_text", "垃圾箱");
		mData.add(hm);
		
		hm = new HashMap<String, Object>();
		hm.put("item_image", R.drawable.location);
		hm.put("item_text", "位置");
		mData.add(hm);
		
		hm = new HashMap<String, Object>();
		hm.put("item_image", R.drawable.settings);
		hm.put("item_text", "设置");
		mData.add(hm);
	}
	
	@Override
	public void onResume() {
		
		Log.e(TAG, "onResume is called!");
		
		super.onResume();
		
		updateTrash();
	}
	
	private void launchActivity(int i) {
		
		Intent itt;
		
		switch (i) {
		
    		case 0:
    			
    			itt = new Intent(mMain, ViewTaskAct.class);
    			
    			itt.putExtra("list_index", -1);
    			itt.putExtra("task_status", C.TASK.COMPLETED);
    
    			startActivity(itt);
    			
    			break;
    			
    		case 1:
    			
    			itt = new Intent(mMain, ViewTaskAct.class);
    			
				itt.putExtra("list_index", -1);
				itt.putExtra("task_status", C.TASK.CANCELLED);
	
				startActivity(itt);
    			
    			break;
		
    		case 2:			// location
    			
    			itt = new Intent(mMain, MgrPositionAct.class);
    			startActivity(itt);
    			
    			break;
    			
    		case 3:			// settings
    			
    			itt = new Intent(mMain, SettingAct.class);
    			startActivity(itt);
    			
    			break;
    			
    		default:
    			break;
		}
		
	}
	
}
